import {routes} from "@/utils/routes";
import {useLocationStore} from "@/stores/locationStore";
import type {Pinia} from 'pinia';

const locationStore = () => {
    const nuxtApp = useNuxtApp();
    return useLocationStore(nuxtApp.$pinia as Pinia);
};

/**
 * 日期格式化
 * @param dateString 要格式化的日期字符串 {e.g. 2024-11-01 18:16:03}
 */
export const formatDate = (dateString: string) => {
    const date = new Date(dateString);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
};

/**
 * 路径转换为 kebab-case 名称
 * @param path 要转换的路径
 */
export const convertPathToKebabCaseName = (path: string) => {
    return path
        .replace(/^\//, '')               // 去掉开头的斜杠
        .split('/')                        // 分割路径
        .join('-');                         // 用连字符连接
}

/**
 * 根据路由名称获取对应的路径和归属
 * @param name 路由名称
 */
export const getRouteInfoByName = (name: string) => {
    return findRouteInfo(route => route.name === name);
};

/**
 * 根据路由路径获取对应的路径和归属
 * @param path 路由路径
 */
export const getRouteInfoByPath = (path: string) => {
    return findRouteInfo(route => route.path === path);
};

/**
 * 根据给定的比对函数找到匹配的路由信息
 * @param predicate 比对函数
 */
const findRouteInfo = (predicate: (route: any) => boolean) => {
    for (const category in routes) {
        for (const route of routes[category]) {
            if (predicate(route)) {
                return {
                    name: route.name,
                    displayName: route.displayName,
                    englishName: route.englishName,
                    routeName: convertPathToKebabCaseName(route.path),
                    path: route.path,
                    belong: route.belongTo
                };
            }
        }
    }
    return null;
};

/**
 * 处理链接点击，更新 store
 * @param path 路由路径
 */
export const setLocationByPath = (path: string) => {
    setLocation(getRouteInfoByPath(path))
    return path
};

/**
 * 处理链接点击，更新 store
 */
export const setLocationByName = (name: string) => {
    setLocation(getRouteInfoByPath(findPathByName(name)))
};

const setLocation = (routeInfo: {
    name: string,
    displayName: string | undefined,
    englishName: string | undefined,
    routeName: string,
    path: string,
    belong: string
} | null) => {
    if (routeInfo) {
        const store = locationStore();
        store.name = routeInfo.name;
        if (routeInfo.displayName) {
            store.displayName = routeInfo.displayName;
        }
        if (routeInfo.englishName) {
            store.englishName = routeInfo.englishName;
        }
        store.routeName = routeInfo.routeName;
        store.path = routeInfo.path;
        store.belongTo = routeInfo.belong;
    }
}

/**
 * 该函数创建一个临时的 div 元素，并将包含 HTML 内容的字符串作为 innerHTML 设置给它。浏览器会自动将 HTML 转换为 DOM 元素，随后我们可以使用 textContent 提取纯文本内容。
 */
export const stripHTML = (html: string) => {
    const tempDiv = document.createElement('div');
    tempDiv.innerHTML = html;
    return tempDiv.textContent || tempDiv.innerText || '';
}